﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;
using He.Common;

namespace He.Web.manage.ad
{
    public partial class position_edit : He.Web.UI.Manage
    {
        private string action = Enums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = GetString("action");
            if (!string.IsNullOrEmpty(_action) && _action == Enums.ActionEnum.Edit.ToString())
            {
                this.action = Enums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                    return;
                }
                if (!new He.BLL.ad_position().Exists(this.id))
                {
                    JscriptMsg(350, 230, "错误提示", "信息不存在或已被删除！", "back", "Error");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ddlLang.DataSource = LangHelper.GetLangDict();
                ddlLang.DataTextField = "Value";
                ddlLang.DataValueField = "Key";
                ddlLang.DataBind();

                ddlDisplayEffect.DataSource = EnumHelper.GetList(typeof(Enums.FocusEffect));
                ddlDisplayEffect.DataTextField = "Text";
                ddlDisplayEffect.DataValueField = "Text";
                ddlDisplayEffect.DataBind();

                if (action == Enums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 赋值操作
        private void ShowInfo(int _id)
        {
            He.BLL.ad_position bll = new BLL.ad_position();
            He.Model.ad_position model = bll.GetModel(_id);
            this.txtTitle.Text = model.title;
            this.txtCallMark.Text = model.call_mark;
            this.rblAdStatus.SelectedValue = model.ad_status.ToString();
            this.txtAdWidth.Text = model.ad_width.ToString();
            this.txtAdHeight.Text = model.ad_height.ToString();
            this.rblAdTarget.SelectedValue = model.ad_target;
            this.ddlDisplayEffect.SelectedValue = model.display_effect;
            this.rblAllowSetEffect.SelectedValue = model.allow_set_effect.ToString().ToLower();
            this.ddlLang.SelectedValue = model.lang;
        }
        #endregion

        #region 增加操作
        private bool DoAdd()
        {

            Model.ad_position model = new Model.ad_position();
            BLL.ad_position bll = new BLL.ad_position();
            model.title = this.txtTitle.Text.Trim();
            model.call_mark = this.txtCallMark.Text.Trim();
            model.ad_status = int.Parse(this.rblAdStatus.SelectedValue);
            model.ad_width = this.txtAdWidth.Text;
            model.ad_height = this.txtAdHeight.Text;
            model.ad_target = this.rblAdTarget.SelectedValue;
            model.display_effect = this.ddlDisplayEffect.SelectedValue;
            model.allow_set_effect = int.Parse(this.rblAllowSetEffect.SelectedValue);
            model.lang = ddlLang.SelectedValue;
            if (bll.Add(model) > 0)
            {
                //更新缓存
                RemoveAdCache(model.call_mark);
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作
        private bool DoEdit(int _id)
        {
            He.BLL.ad_position bll = new He.BLL.ad_position();
            He.Model.ad_position model = bll.GetModel(_id);

            model.title = this.txtTitle.Text.Trim();
            model.call_mark = this.txtCallMark.Text.Trim();
            model.ad_status = int.Parse(this.rblAdStatus.SelectedValue);
            model.ad_width = this.txtAdWidth.Text;
            model.ad_height = this.txtAdHeight.Text;
            model.ad_target = this.rblAdTarget.SelectedValue;
            model.display_effect = this.ddlDisplayEffect.SelectedValue;
            model.allow_set_effect = int.Parse(this.rblAllowSetEffect.SelectedValue);
            model.lang = ddlLang.SelectedValue;
            if (bll.Update(model))
            {
                //更新缓存
                RemoveAdCache(model.call_mark);
                return true;
            }
            return false;
        }
        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (action == Enums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {
                    JscriptPrint("保存过程中发生错误啦！", "position_list.aspx", "Error");
                    return;
                }
                JscriptPrint("广告位修改成功啦！", "position_list.aspx", "Success");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptPrint("保存过程中发生错误啦！", "position_list.aspx", "Error");
                    return;
                }
                JscriptPrint("广告位增加成功啦！", "position_list.aspx", "Success");
            }
        }
    }
}